<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\model\Credit;
use think\Request;

class CreditController extends BaseController
{
	function createCredit()
	{
		$name = $this->request->post('name');
		$mobile = $this->request->post('mobile');
		Credit::create([
			'user_id' => $this->user_id,
			'name' => $name,
			'mobile' => $mobile,
		]);
		$this->writeJson();
	}
	
	#贷款
	function credit()
	{
		$credit_amount = $this->request->get('credit_amount');
		$year = $this->request->get('year/d');
		$rate = $this->request->get('rate');
		$calculateEqualInstallmentPayment = $this->calculateEqualInstallmentPayment($credit_amount, $year, $rate);//等额本息
		$calculateEqualPrincipalPayment = $this->calculateEqualPrincipalPayment($credit_amount, $year, $rate);//等额本金
		$this->writeJson([
			'benxi' => $calculateEqualInstallmentPayment,
			'benjin' => $calculateEqualPrincipalPayment,
		]);
	}
	
	#组合贷款
	function combination()
	{
		$credit_amount = $this->request->get('credit_amount');
		$credit_rate = $this->request->get('credit_rate');
		$fund_amount = $this->request->get('fund_amount');
		$fund_rate = $this->request->get('fund_rate');
		$year = $this->request->get('year/d');
		
		$calculateCombinationLoan = $this->calculateCombinationLoan($credit_amount, $year, $credit_rate, $fund_amount, $year, $fund_rate); //等额本息
		$calculatePrincipaltionLoan = $this->calculatePrincipaltionLoan($credit_amount, $year, $credit_rate, $fund_amount, $year, $fund_rate); //等额本金
		
		$this->writeJson([
			'benxi' => $calculateCombinationLoan,
			'benjin' => $calculatePrincipaltionLoan,
		]);
	}
	
	// （等额本金）
	//$loanAmount=>贷款金额
	//$loanPeriod=>贷款年限
	//$interestRate=>贷款利息
	function calculateEqualPrincipalPayment($loanAmount, $loanPeriod, $interestRate)
	{
		$numOfMonthlyPayments = $loanPeriod * 12;
		$monthlyPrincipal = round($loanAmount / $numOfMonthlyPayments, 2);
		$monthlyInterest = round($loanAmount * ($interestRate / 100) / 12,2);
		$remainingAmount = $loanAmount;
		$result = [];
		$interest_all = 0;
		$monthly_payment_all = 0;
		$loan_amount = $loanAmount;
		
		for ($i = 1; $i <= $numOfMonthlyPayments; $i++) {
			$interest = round($remainingAmount * ($interestRate / 100) / 12, 2);
			$principal = $monthlyPrincipal;
			$remainingAmount = round($remainingAmount - $monthlyPrincipal,2);
			$monthlyPayment = round($principal + $interest, 2);
			
			
			if ($remainingAmount < 1) {
				$interest = round($interest + $remainingAmount, 2);
				$remainingAmount = 0;
			}
			
			$result[] = [
				'month' => $i,//期数
				'principal' => $principal,//月供本金
				'interest' => $interest,//月供利息
				'monthlyPayment' => $monthlyPayment,//月供总额
				'remainingAmount' => $remainingAmount,//剩余本金
			];
			$interest_all += $interest;
			$monthly_payment_all += $monthlyPayment;
		}
		
		return [
			'monthly_payment' => $monthlyInterest,//首月还款
			'interest_all' => round($interest_all,2),//支付利息
			'monthly_payment_all' => round($monthly_payment_all,2),//还款总额
			'loan_amount' => $loan_amount,//贷款总额
			'num_of_monthly' => $numOfMonthlyPayments,//贷款月数
			'list' => $result
		];
	}
	
	// （等额本息）
	//$loanAmount=>贷款金额
	//$loanPeriod=>贷款年限
	//$loanPeriod=>贷款利率
	function calculateEqualInstallmentPayment($loanAmount, $loanPeriod, $interestRate)
	{
		$monthlyInterestRate = $interestRate / 12 / 100;
		$numOfMonthlyPayments = $loanPeriod * 12;
		
		$monthlyPayment = round($loanAmount * $monthlyInterestRate * pow(1 + $monthlyInterestRate, $numOfMonthlyPayments) / (pow(1 + $monthlyInterestRate, $numOfMonthlyPayments) - 1), 2);
		
		$result = [];
		
		$interest_all = 0;
		$monthly_payment_all = 0;
		$loan_amount = $loanAmount;
		for ($i = 1; $i <= $numOfMonthlyPayments; $i++) {
			$interest = round($loanAmount * $monthlyInterestRate, 2);
			$principal = round($monthlyPayment - $interest, 2);
			$remainingAmount = round($loanAmount - $principal, 2);
			
			$result[] = [
				'month' => $i,//期数
				'principal' => $principal,//月供本金
				'interest' => $interest,//月供利息
				'monthlyPayment' => $monthlyPayment,//月供总额
				'remainingAmount' => $remainingAmount,//剩余本金
			];
			
			$loanAmount = $remainingAmount;
			$interest_all += $interest;
			$monthly_payment_all += $monthlyPayment;
		}
		
		return [
			'monthly_payment' => $monthlyPayment,//月供
			'interest_all' => round($interest_all,2),//支付利息
			'monthly_payment_all' => round($monthly_payment_all,2),//还款总额
			'loan_amount' => $loan_amount,//贷款总额
			'num_of_monthly' => $numOfMonthlyPayments,//贷款月数
			'list' => $result
		];
	}
	
	// 测试组合贷款计算()等额本息
	function calculateCombinationLoan($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate, $fundLoanAmount, $fundLoanPeriod, $fundInterestRate)
	{
		// 计算商业贷款的还款计划
		$commercialRepayments = $this->calculateEqualInstallmentPayment($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate);
		
		// 计算公积金贷款的还款计划
		$fundRepayments = $this->calculateEqualInstallmentPayment($fundLoanAmount, $fundLoanPeriod, $fundInterestRate);
		
		$arr_ti = $commercialRepayments['list'];
		$arr_ti_duan = $fundRepayments['list'];
		
		
		$newArray = [];
		$shangye_interest = 0;
		$jijin_interest = 0;
		$shangye_monthly_payment_all = 0;
		$jijin_monthly_payment_all = 0;
		foreach ($arr_ti as $is => $repayment) {
			$shangye_monthlyPayment = $repayment["monthlyPayment"] ?: 0;
			$jijin_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"] ?: 0;
			$shangye_interest += $repayment["interest"];
			$jijin_interest += $arr_ti_duan[$is]["interest"];
			$shangye_monthly_payment_all += $shangye_monthlyPayment;
			$jijin_monthly_payment_all += $jijin_monthlyPayment;
			$interest = round($repayment["interest"] + $arr_ti_duan[$is]["interest"], 2);
			$monthlyPayment = round($repayment["monthlyPayment"] + $arr_ti_duan[$is]["monthlyPayment"], 2);
			$remainingAmount = round($repayment["remainingAmount"] + $arr_ti_duan[$is]["remainingAmount"], 2);
			
			if ($remainingAmount < 1) {
				$interest = round($interest + $remainingAmount, 2);
				$remainingAmount = 0;
			}
			$newArray[] = [
				"month" => $repayment["month"], //月份
				"jijin_monthlyPayment" => round($jijin_monthlyPayment, 2), //公积金还款
				"shangye_monthlyPayment" => round($shangye_monthlyPayment, 2),//商业还款
				"monthlyPayment" => $monthlyPayment,//月供
				"interest" => $interest,//月利息
				"monthly" => round($monthlyPayment - $interest, 2),//还款本金
				"remainingAmount" => $remainingAmount,//剩余还款
			];
		}
		return [
			'shangye' => [
				'monthly_payment' => $commercialRepayments['monthly_payment'],
				'interest' => round($shangye_interest,2),
				'monthly_payment_all' => round($shangye_monthly_payment_all,2),
				'loan_amount' => $commercialLoanAmount,
				'num_of_monthly' => $commercialLoanPeriod * 12
			],
			'jijin' => [
				'monthly_payment' => $fundRepayments['monthly_payment'],
				'interest' => round($jijin_interest,2),
				'monthly_payment_all' => round($jijin_monthly_payment_all,2),
				'loan_amount' => $fundLoanAmount,
				'num_of_monthly' => $fundLoanPeriod * 12
			],
			'list' => $newArray
		];
	}
	
	
	// 测试组合贷款计算(等额本金)
	function calculatePrincipaltionLoan($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate, $fundLoanAmount, $fundLoanPeriod, $fundInterestRate)
	{
		// 计算商业贷款的还款计划
		$commercialRepayments = $this->calculateEqualPrincipalPayment($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate);
		
		// 计算公积金贷款的还款计划
		$fundRepayments = $this->calculateEqualPrincipalPayment($fundLoanAmount, $fundLoanPeriod, $fundInterestRate);
		
		$arr_ti = $commercialRepayments['list'];
		$arr_ti_duan = $fundRepayments['list'];
		
		
		$newArray = [];
		$shangye_interest = 0;
		$jijin_interest = 0;
		$shangye_monthly_payment_all = 0;
		$jijin_monthly_payment_all = 0;
		foreach ($arr_ti as $is => $repayment) {
			$shangye_monthlyPayment = $repayment["monthlyPayment"] ?: 0;
			$jijin_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"] ?: 0;
			$shangye_interest += $repayment["interest"];
			$jijin_interest += $arr_ti_duan[$is]["interest"];
			$shangye_monthly_payment_all += $shangye_monthlyPayment;
			$jijin_monthly_payment_all += $jijin_monthlyPayment;
			$interest = round(($repayment["interest"] + $arr_ti_duan[$is]["interest"]), 2);
			$monthlyPayment = round($repayment["monthlyPayment"] + $arr_ti_duan[$is]["monthlyPayment"], 2);
			
			$remainingAmount = round(($repayment["remainingAmount"] + $arr_ti_duan[$is]["remainingAmount"]), 2);
			
			if ($remainingAmount < 1) {
				$interest = round($interest + $remainingAmount, 2);
				$remainingAmount = 0;
			}
			
			$newArray[] = [
				"month" => $repayment["month"],
				"jijin_monthlyPayment" => round($jijin_monthlyPayment, 2),
				"shangye_monthlyPayment" => round($shangye_monthlyPayment, 2),
				"monthlyPayment" => $monthlyPayment,
				"interest" => $interest,
				"remainingAmount" => $remainingAmount,
			];
		}
		return [
			'shangye' => [
				'monthly_payment' => $commercialRepayments['monthly_payment'],
				'interest' => round($shangye_interest,2),
				'monthly_payment_all' => round($shangye_monthly_payment_all,2),
				'loan_amount' => $commercialLoanAmount,
				'num_of_monthly' => $commercialLoanPeriod * 12
			],
			'jijin' => [
				'monthly_payment' => $fundRepayments['monthly_payment'],
				'interest' => round($jijin_interest,2),
				'monthly_payment_all' => round($jijin_monthly_payment_all,2),
				'loan_amount' => $fundLoanAmount,
				'num_of_monthly' => $fundLoanPeriod * 12
			],
			'list' => $newArray
		];
	}
}
